package JavaSEAdvance.Lanlords;

import java.util.*;

/**
 * @author Xiao
 * @date 2021/7/28
 */
public class LanlordsDemo03 {
    /**
     * 模拟斗地主升级版
     * 通过程序实现斗地主过程中的洗牌、发牌和看牌。要求：对牌进行排序。
     * ♠   ♥   ♦   ♣    King    Queen
     */
    @SuppressWarnings("all")
    public static void main(String[] args) {
        String[] color = {"♠", "♥", "♦", "♣"};
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2"};
        HashMap<Integer, String> poker = new HashMap<>();   //创建一个HashMap集合poker作为牌盒，存储编号和纸牌
        ArrayList<Integer> list = new ArrayList<>();    //创建一个ArrayList集合list接收编号
        //将编号和纸牌存入poker中，将编号存入list中
        int index = 0;
        for (String num : number) {
            for (String col : color) {
                list.add(index);
                poker.put(index, col + num);
                index++;
            }
        }
        list.add(index);
        poker.put(index, "Queen");
        index++;
        list.add(index);
        poker.put(index, "King");
        //打乱list中的编号顺序，并将list集合中的编号分成4组放到上述4个集合中
        Collections.shuffle(list);

        /*//1.利用ArrayList集合存储玩家的纸牌编号和底牌编号并看牌
        arrayListStoreAndlookCards(poker, list);*/

        //2.利用TreeSet集合存储玩家的纸牌编号和底牌编号并看牌
        treeSetStoreAndlookCards(poker, list);
    }

    /**利用ArrayList集合存储玩家的纸牌编号和底牌编号并看牌*/
    private static void arrayListStoreAndlookCards(HashMap<Integer, String> poker, ArrayList<Integer> list) {
        //创建4个ArrayList集合，分别代表3个玩家的编号和底牌的编号
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        ArrayList<Integer> list3 = new ArrayList<>();
        ArrayList<Integer> listCards = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size() - 3) {
                listCards.add(list.get(i));
            } else if (i % 3 == 0) {
                list1.add(list.get(i));
            } else if (i % 3 == 1) {
                list2.add(list.get(i));
            } else {
                list3.add(list.get(i));
            }
        }
        //调用看牌方法，查看玩家的牌和底牌
        arrayListLookCards(poker, list1, list2, list3, listCards);
    }

    /**利用Set集合存储玩家的纸牌编号和底牌编号并看牌*/
    private static void treeSetStoreAndlookCards(HashMap<Integer, String> poker, ArrayList<Integer> list) {
        TreeSet<Integer> ts1 = new TreeSet<>();
        TreeSet<Integer> ts2 = new TreeSet<>();
        TreeSet<Integer> ts3 = new TreeSet<>();
        TreeSet<Integer> tsCards = new TreeSet<>();
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size() - 3) {
                tsCards.add(list.get(i));
            } else if (i % 3 == 0) {
                ts1.add(list.get(i));
            } else if (i % 3 == 1) {
                ts2.add(list.get(i));
            } else {
                ts3.add(list.get(i));
            }
        }
        treeSetLookCards(poker,ts1,"阿良");
        treeSetLookCards(poker,ts2,"左右");
        treeSetLookCards(poker,ts3,"平安");
        treeSetLookCards(poker,tsCards,"底牌");
    }

    /**定义一个方法：然后根据编号和玩家名字查看对应的纸牌*/
    private static void treeSetLookCards(HashMap<Integer, String> poker, TreeSet<Integer> ts1, String name) {
        System.out.print(name + "：");
        for (Integer integer : ts1) {
            System.out.print(poker.get(integer) + "  ");
        }
        System.out.println();
    }

    /**定义一个方法：对玩家的编号和底牌编号排序，然后根据编号查看纸牌*/
    public static void arrayListLookCards(HashMap<Integer, String> hm, ArrayList<Integer>... arr) {
        for (int i = 0; i < arr.length; i++) {
            Collections.sort(arr[i]);
            if (i == arr.length - 1) {
                System.out.print("底牌：");
            } else {
                System.out.print("玩家" + (i + 1) +"的牌：");
            }
            for (Integer integer : arr[i]) {
                System.out.print(hm.get(integer) + "  ");
            }
            System.out.println();
        }
    }
}
